﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

// HonorBuddy Includes
using Styx;
using Styx.Plugins;
using Styx.WoWInternals;
using Styx.WoWInternals.World;
using Styx.WoWInternals.WoWObjects;
using Styx.Common;
using Styx.Common.Helpers;
using Styx.Pathing;



namespace GotWood_Plugin
{
    public class GotWood : HBPlugin
    {
        #region Globals

        public override string Name { get { return "Got Wood? - Locate & Harvest Timber"; } }
        private static string LogName { get { return "GotWood"; } }
        public override string Author { get { return "FaceRollFTW"; } }
        public override Version Version { get { return _version; } }
        private readonly Version _version = new Version(1, 0, 0, 0);
        private static LocalPlayer Me { get { return StyxWoW.Me; } }


        #region Config Form

        public override string ButtonText { get { return "Settings"; } }
        public override bool WantButton { get { return true; } }

        public static GW_Settings GoldSettings = new GW_Settings();

        public override void OnButtonPress()
        {
            GW_Config ConfigForm = new GW_Config(GoldSettings);
            ConfigForm.ShowDialog();

            GoldSettings.DumpSettings();
            GoldSettings.Save();

        }

        #endregion

        private enum TimberType
        {
            SmallAlliance = 234193,
            SmallHorde = 234097,        // <GameObject Name="Small Timber" Entry="234097" />
            MediumAlliance = 234194,
            MediumHorde = 234098,       // <GameObject Name="Timber" Entry="234098" />
            LargeAlliance = 234195,
            LargeHorde = 234099         // <GameObject Name="Large Timber" Entry="234099" />
        };

        private static readonly HashSet<uint> ValidZones = new HashSet<uint>
        {
            // 6941,   // Ashran
            6720,   // Frostfire Ridge
            6721,   // Gorgrond
            6755,   // Nagrand
            6719,   // Shadowmoon Valley
            6722,   // Spires Of Arak
            6662   // Talador
        };


        #endregion


        public override void OnEnable()
        {
            base.OnEnable();

            // MyDebugging("OnEnable: Settings file name = " + GoldSettings.SettingsPath);

            Logging.Write(LogLevel.Normal, Colors.Orange, "[{0} {1}]: Enabled", LogName, Version);
        }

        public override void OnDisable()
        {
            base.OnDisable();

            Logging.Write(LogLevel.Normal, Colors.Orange, "[{0} {1}]: Disabled", LogName, Version);
        }



        //
        // Executed everytime the Plugin is pulsed.
        //
        public override void Pulse()
        {
            WoWGameObject Timber = null;


            if (Me.Combat || Me.IsCasting || Trees.Count() == 0 || !IsValidZone)
            {
                return;
            }

            Timber = Trees.FirstOrDefault();

            if (Timber.Distance > Timber.InteractRange)
            {
                MyLogging("Moving to {0} at location X = {1}, Y = {2}, Z = {3}", Timber.Name, Timber.Location.X.ToString(), Timber.Location.Y.ToString(), Timber.Location.Z.ToString());
                Navigator.MoveTo(Timber.Location);
            }
            else
            {
                MyLogging("Interacting with {0} at location X = {1}, Y = {2}, Z = {3}", Timber.Name, Timber.Location.X.ToString(), Timber.Location.Y.ToString(), Timber.Location.Z.ToString());
                Timber.Interact();
            }
            return;
        }


        private bool IsValidZone
        {
            get
            {
                if (ValidZones.Contains(Me.ZoneId))
                    return true;
                else
                    return false;
            }
        }


        private IOrderedEnumerable<WoWGameObject> Trees
        {
            get
            {
                // Find the nearest timbers

                var TreeList = ObjectManager.GetObjectsOfType<WoWGameObject>(false, false)
                            .Where(wgo => (
                                    ((wgo.Entry == (uint)TimberType.SmallAlliance) && GoldSettings.getSmallTimber) ||
                                    ((wgo.Entry == (uint)TimberType.SmallHorde) && GoldSettings.getSmallTimber) ||
                                    ((wgo.Entry == (uint)TimberType.MediumAlliance) && GoldSettings.getMediumTimber) ||
                                    ((wgo.Entry == (uint)TimberType.MediumHorde) && GoldSettings.getMediumTimber) ||
                                    ((wgo.Entry == (uint)TimberType.LargeAlliance) && GoldSettings.getLargeTimber) ||
                                    ((wgo.Entry == (uint)TimberType.LargeHorde) && GoldSettings.getLargeTimber)
                                    ))
                            .OrderBy(wgo => wgo.Distance);

                MyDebugging("TreeList.Count = {0}", TreeList.Count().ToString());

                return TreeList;
            }
        }

        private static void MyLogging(string Message, params object[] args)
        {
            Logging.Write(LogLevel.Normal, Colors.Orange, LogName + ": " + Message, args);
        }

        private static void MyDebugging(string Message, params object[] args)
        {
            if (GoldSettings.DebugLog == true)
                Logging.Write(LogLevel.Normal, Colors.Aqua, LogName + " Debug: " + Message, args);
        }

    }
}
